package hsintiao.com.xintiaoAPP.service.impl;

import hsintiao.com.xintiaoAPP.entity.Article;
import hsintiao.com.xintiaoAPP.entity.ArticleType;
import hsintiao.com.xintiaoAPP.entity.vo.ArticlePageVO;
import hsintiao.com.xintiaoAPP.entity.vo.ArticleVO;
import hsintiao.com.xintiaoAPP.mapper.ArticleMapper;
import hsintiao.com.xintiaoAPP.mapper.ArticleTypeMapper;
import hsintiao.com.xintiaoAPP.service.ArticleService;
import hsintiao.com.xintiaoAPP.util.AjaxResult;
import hsintiao.com.xintiaoAPP.util.GetDifferTime;
import hsintiao.com.xintiaoAPP.util.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    /*
    * 获取所有文章(暂不使用)
    * */
    @Override
    public AjaxResult allArticle() {
        Date now = new Date();
        ArticleVO articleVO = new ArticleVO();
        try {
            /*List<ArticleType> allType = articleTypeMapper.allType();
            List<ArticleType> articlesByType = new ArrayList<>();
            for (ArticleType type:articleTypes) {
                //循环类型列表，获取每个类型对应的所有文章列表
                List<Article> articleByType = articleMapper.getArticleByType(type.getId());
                //设置发布时间格式
                List<Article> articles = this.changeTime(articleByType, now);
                //设置每个类型的文章列表数据
                type.setArticleList(articles);
                //添加到VO对象中的类型列表(该列表中的每个类型对象都包含对应的所有文章列表数据)
                articlesByType.add(type);
            }
            articleVO.setArticles();

            //List<Article> allArticle = articleMapper.allArticle();

            //非空判断
            if(articleVO == null || articleVO.getArticlesByType().size()==0){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }*/
            //设置发布时间
            //List<Article> articles = this.changeTime(allArticle,now);
            //查询轮播图放入VO对象中
            List<String> carousel = articleMapper.getCarousel();
            articleVO.setCarousel(carousel);
            if(carousel == null || carousel.size()==0){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            /*articleVO.setArticles(articles);
            articleVO.setCarousel(carousel);*/

            return AjaxResult.me().setResultObj(articleVO);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("查询失败");
        }
    }

    /*
    * 根据类型获取所有文章
    * */
    @Override
    public AjaxResult getArticleByType(Long typeId) {
        Date now = new Date();
        try {
            ArticleVO articleVO = new ArticleVO();
            List<Article> articleByType = articleMapper.getArticleByType(typeId);
            System.out.println("articleByType="+articleByType);
            //注意，判断空要使用||而不是&&
            if(articleByType == null || articleByType.size()==0){
                System.out.println("查询失败");
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            List<Article> articles = this.changeTime(articleByType, now);
            List<String> carousel = articleMapper.getCarousel();
            if(carousel == null || carousel.size()==0){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            /*articleVO.setArticles(articles);
            articleVO.setCarousel(carousel);*/
            return AjaxResult.me().setResultObj(articleVO);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("查询失败");
        }
    }

    @Override
    public AjaxResult getOneById(Long id) {
        try {
            Article one = articleMapper.getOneById(id);
            if(one == null || one.equals("")){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            return AjaxResult.me().setResultObj(one);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("查询失败");
        }
    }

    /*
    * 根据keyword关键字查询相应的文章
    * */
    @Override
    public AjaxResult searchByKeyword(String keyword) {
        try {
            List<Article> articles = articleMapper.searchByKeyword(keyword);
            if(articles == null || articles.size()==0){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            List<Article> articleList = this.changeTime(articles, new Date());
            return AjaxResult.me().setResultObj(articleList);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("查询失败");
        }
    }
    /*
    * 分页获取文章指定类型的某一页数据数据
    * */
    @Override
    public AjaxResult getAllByTypeForPage(ArticlePageVO pageVO) {
        try {
            ArticleVO vo = new ArticleVO();
            //获取文章类型
            String typeName = pageVO.getTypeName();
            //System.out.println("typeName="+typeName);
            //前端传递的类型为字符串的类型名称，根据类型名称获取对应的类型，再获取对应的id查找指定类型id下的文章数据
            ArticleType type = articleTypeMapper.selectByName(typeName);
            Long typeID = type.getId();
            //获取页数，设置mybatis的limit的起始和结束索引位置
            Integer page = pageVO.getPage();
            Long start=(long)(page-1)*pageVO.getTotal();
            Long end=(long) pageVO.getTotal();
            //获取轮播图
            List<String> carousel = articleMapper.getCarousel();
            //获取所有的文章类型(文章类型导航栏)
            List<ArticleType> allType = articleTypeMapper.allType();
            //根据文章类型的id和分页下标获取指定的文章数据
            List<Article> articles = articleMapper.selectPageByType(typeID,start,end);
            //设置文章的发布时间格式
            List<Article> articles1 = changeTime(articles, new Date());
            vo.setCarousel(carousel);
            vo.setArticles(articles1);
            vo.setTypes(allType);
            return AjaxResult.me().setResultObj(vo);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("查询失败");
        }
    }

    //用于将文章的发布时间与当前时间的差值做格式转换
    public List<Article> changeTime(List<Article> articles, Date now){
        GetDifferTime getDifferTime = new GetDifferTime();
        for (Article article:articles) {
            Date before = article.getReleaseTime();//获取每一个文章的发布时间
            String time = getDifferTime.getTime(before, now);
            article.setBeforeReleaseTime(time);
        }
        return articles;
    }
}
